import sys
import os
from PySide6 import QtWidgets

from ui.option import OptionWidget
from ui.arithmetic_creator import ArithmeticCreator
from ui.arithmetic_practice import ArithmeticPractice

from util.exercise import Exercise
import util.exercise_file_dao as dao


class MainWindow:
    def __init__(self):
        self.option_widget = OptionWidget()
        self.creator_widget = ArithmeticCreator()
        self.practice_widget = ArithmeticPractice()

        # 绑定事件
        self.option_widget.btn_generate.clicked.connect(self.opt_to_cre)
        self.option_widget.btn_check.clicked.connect(self.handle_check)
        self.option_widget.btn_offline.clicked.connect(self.opt_to_pra)
        self.creator_widget.btn_home.clicked.connect(
            self.cre_to_opt)
        self.practice_widget.btn_home.clicked.connect(
            self.pra_to_opt)
        self.creator_widget.btn_online.clicked.connect(
            self.handle_online)

    # 检查答案
    def handle_check(self):
        # 打开并读取 CSV
        file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
            self.option_widget, "打开习题 CSV 文件", "", "CSV files (*.csv)")

        file_name_with_extension = os.path.basename(file_path)
        file_name, file_extension = os.path.splitext(file_name_with_extension)

        if file_path:
            num, ok = QtWidgets.QInputDialog.getInt(
                self.option_widget, "提示", "输入每行算式的个数：", value=5)
            if not ok:
                print("用户取消输入算式个数")
                return
            equationList, answer = dao.read_exercise_from_file(file_path)
            exercise = Exercise()
            exercise.equationList = equationList
            exercise.answer = answer
            wrongExercise = exercise.check()

            num = num if num else 5
            dao.write_exercise_to_file(
                f'{file_name}-fail.csv', num, wrongExercise)
            dao.write_check_to_file(
                f'{file_name}-statistic.csv', wrongExercise)

    # 在线答题
    def handle_online(self):
        self.creator_widget.hide()
        self.practice_widget.setWindowTitle("在线答题")
        self.practice_widget.set_exercise_and_start(
            self.creator_widget.exercise)
        self.practice_widget.show()
        # print(self.arithmetic_creator_widget.exercise) # 已测试，可以生成习题

    def opt_to_cre(self):
        self.option_widget.hide()
        self.creator_widget.show()

    def opt_to_pra(self):
        file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
            self.option_widget, "打开习题 CSV 文件", "", "CSV files (*.csv)")
        if file_path:
            equationList, answer = dao.read_exercise_from_file(file_path)
            exercise = Exercise()
            exercise.equationList = equationList
            self.practice_widget.set_exercise_and_start(exercise)

            self.option_widget.hide()
            self.practice_widget.setWindowTitle("离线答题")
            self.practice_widget.show()

    def cre_to_opt(self):
        self.creator_widget.hide()
        self.option_widget.show()

    def pra_to_opt(self):
        self.practice_widget.hide()
        self.option_widget.show()


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.option_widget.show()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()
